import {Component, Input, OnInit} from '@angular/core';
import {Member} from "../../../../model/Member";
import {ManageService} from "../../../../services/manage.service";
import {AlertService} from "../../../../services/alert.service";
import {NzModalRef} from "ng-zorro-antd/modal";

@Component({
  selector: 'app-glyedit',
  templateUrl: './glyedit.component.html',
  styleUrls: ['./glyedit.component.css']
})
export class GlyeditComponent implements OnInit {

  @Input() optype: string;
  @Input() deptid: string;
  @Input() adminid: string;
  a: Member;
  gl: Array<any>;

  constructor(private manageService: ManageService
    , private alertservice: AlertService
    , private modal: NzModalRef) {
    this.a = new Member();
    this.gl = new Array<any>();

  }

  ngOnInit(): void {
    this.loadGroupSel();
    if (this.optype == 'add') {
      this.a.dept = this.deptid;
    }
    else if (this.optype == 'update') {
      this.getAdminById();
    }

  }

  getAdminById() {
    this.manageService.getMemberById(this.adminid)
      .then((data: any) => {
        if (data.msg == 'ok') {
          this.a = new Member();
          this.a = data.obj;
        }
        else {
          this.alertservice.showError('获取管理员信息异常，请重试');
          this.modal.close('error');
        }
      })
  }


  save() {
    this.manageService.saveMember(this.a)
      .then((data: any) => {
        if (data.msg == 'ok') {

        } else if (data.msg == "namerepeat") {
          this.alertservice.showWarning("登录名重复")
        }
        else {
          this.alertservice.showError('保存管理员异常，请重试');
        }
      })
  }

  update() {
    this.manageService.updateMember(this.a)
      .then((data: any) => {
        if (data.msg == 'ok') {

        } else if (data.msg == "namerepeat") {
          this.alertservice.showWarning("登录名重复")
        }
        else {
          this.alertservice.showError('保存管理员异常，请重试');
        }
      })
  }


  loadGroupSel() {
    this.manageService.getRoleGroupNzSelList()
      .then((data: any) => {
        if (data.msg == 'ok') {
          this.gl = new Array<any>();
          this.gl = data.list;
        }
        else {
          this.alertservice.showError('获取岗位信息异常，请重试');
        }
      })
  }
}
